<div class="layui-card">
	<div class="layui-card-header">
		<h2 class="header-title">医生管理</h2>
		<span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>医生管理</cite></a>
        </span>
	</div>
	<div class="layui-card-body">
		<div class="layui-form toolbar">
			搜索：<input id="doctor-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字" />&emsp;
			<button id="doctor-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
			<button id="doctor-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button> &emsp;
		</div>

		<!-- 数据表格 -->
		<table class="layui-table" id="doctor-table" lay-filter="doctor-table"></table>
	</div>
</div>
<script type="text/html" id="doctor-model">
	<form id="doctor-form" lay-filter="doctor-form" class="layui-form model-form">
		<input name="yhid" id="yhid" type="hidden" />
		<input name="roleId" type="hidden" />
		<div class="layui-form-item">
			<label class="layui-form-label">员工名称</label>
			<div class="layui-input-block">
				<select name="ygid" id="ygxz" lay-verify="required" lay-filter="yg">
				</select>
			</div>
		</div>

		<div class="layui-form-item model-form-footer">
			<button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
			<button class="layui-btn" lay-filter="doctor-form-submit" lay-submit>保存</button>
		</div>
	</form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="doctor-table-bar">
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
	layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects', 'authtree'], function() {
		var form = layui.form;
		var table = layui.table;
		var config = layui.config;
		var layer = layui.layer;
		var util = layui.util;
		var admin = layui.admin;
		var authtree = layui.authtree;
		var formSelects = layui.formSelects;
		var alog = config.getTempData('loginInfo');

		showdoctorList()

		function showdoctorList() {
			$.ajax({
				type: "POST",
				url: getCustomer,
				data: {},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {},
				success: function(data) {
					console.log(data)
					//渲染表格
					table.render({
						elem: '#doctor-table',
						data: data,
						page: false,
						cols: [
							[{
									field: 'customerServiceId',
									title: '编号'
								},
								{
									field: 'name',
									title: '用户名'
								},
								{
									field: 'nicename',
									title: '医生名称'
								},
								{
									field: 'orgName',
									title: '所属机构'
								},
								{
									field: 'gmtCreate',
									templet: function(d) {
										return d.addTime.split(" ")[0]
									},
									title: '创建时间'
								},
								{
									align: 'center',
									toolbar: '#doctor-table-bar',
									title: '操作'
								}
							]
						]
					});

				},
				beforeSend: function() {},
				complete: function() {},

				error: function(result) {
					console.log(result);
				},
			})

		}

		// 添加按钮点击事件
		$('#doctor-btn-add').click(function() {
			showEditModel();
		});

		var fx = ""
		// 表单提交事件
		form.on('submit(doctor-form-submit)', function(data) {

			console.log(data.field)

			tjys(saveCustomer, data.field)
			return false;
		});

		function tjys(paths, datas) {
			$.ajax({
				type: "POST",
				url: paths,
				data: {
					params: JSON.stringify({
						userId: datas.yhid,
						sysUserId: datas.ygid.split(",")[0]
					})
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					console.log(data)
					if(data.code == 0) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: 1
						});
						showdoctorList()
					} else {
						layer.msg(data.msg, {
							icon: 2
						});
					}
				},
				complete: function() {},
				error: function(result) {
					console.log(result);
				},
			});
		}

		// 工具条点击事件
		table.on('tool(doctor-table)', function(obj) {
			var data = obj.data;
			if(obj.event === 'edit') { //修改
				showEditModel(data);
			} else if(obj.event === 'del') { //删除
				doDelete(obj);
			}
		});

		// 搜索按钮点击事件
		$('#doctor-btn-search').click(function() {

		});

		// 显示编辑弹窗
		var showEditModel = function(datas) {
			$.ajax({
				type: "POST",
				url: path + '/sys/user/list1',
				data: {
					sortOrder: "asc",
					pageSize: 1000,
					pageNumber: 1,
					roleName: ''
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {},
				success: function(data) {
					layer.open({
						type: 1,
						title: '添加医生',
						area: '450px',
						offset: '120px',
						content: $('#doctor-model').html(),
						success: function(d) {
							console.log(d)
							$('#doctor-form')[0].reset();
							$('#doctor-form').attr('method', 'POST');
							var select = document.getElementById("ygxz")
							select.options.add(new Option("请选择", ''))
							for(var i = 0; i < data.rows.length; i++) {
								select.options.add(new Option(data.rows[i].username, data.rows[i].userId + "," + data.rows[i].mobile));
							}

							form.on('select(yg)', function(data) {
								a(data.value.split(",")[1])
							});

							console.log($("#selectu"))
							var x = 0
							window.a = function(uname) {
								$.ajax({
									type: "POST",
									url: userListPath,
									data: {
										params: JSON.stringify({
											sortOrder: "u.user_id asc",
											sortOrder1: "",
											orgId: 1 + "",
											pageSize: 1000,
											pageNumber: 1,
											username: uname,
										})
									},
									dataType: "json",
									async: false,
									xhrFields: {
										withCredentials: true
									},
									beforeSend: function(request) {},
									success: function(da) {
										console.log(da)
										if(da.rows.length == 0) {
											layer.msg("该员工手机号异常", {
												icon: 2
											})
										}
										$("#yhid").val(da.rows[0].userId)
									},
									error: function(result) {
										location.replace('login.html');
									},
								});
							}
							form.render();
						}
					});
					// 渲染多选下拉框

				},
				beforeSend: function() {},
				complete: function() {},

				error: function(result) {
					console.log(result);
				},
			})

		};

		// 删除
		var doDelete = function(obj) {
			layer.confirm('确定要删除吗？', function(i) {
				layer.close(i);
				layer.load(2);
				$.ajax({
					type: "POST",
					url: delCustomer,
					data: {
						customerServiceId: obj.data.customerServiceId
					},
					dataType: "json",
					xhrFields: {
						withCredentials: true
					},
					success: function(data) {
						console.log(data);
						if(data.code == 0) {
							layer.msg(data.msg, {
								icon: 1
							});
							layer.closeAll();
							showdoctorList()
						} else {
							layer.msg(data.msg, {
								icon: 2
							});
						}
					},
					error: function(result) {
						console.log(result);
					},
				});
			});
		};

	});
</script>